package ken.app.adapter;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import ken.app.bean.QueryMemberContentBean;
import ken.app.bean.TreeSingleBean;
import com.she.olympic.R;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.AnimationDrawable;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.she.component.adapter.GenView;
import com.she.component.adapter.GeneralAdapter;
import com.she.component.adapter.iface.IGenView;
import com.she.olympic.TreeActivity;

public class QueryTreeAdapter extends GeneralAdapter {
	
	static ArrayList<TreeSingleBean> mDataList;
	static Handler mhandler;
	static GenSelectAllListItemView mGenSelectAllView = new GenSelectAllListItemView();
	
	public QueryTreeAdapter(Context context, List<?> dataList ,Handler handler) {
		super(context, mGenSelectAllView);
		mDataList = (ArrayList<TreeSingleBean>) dataList;
		mhandler = handler;
		this.setData(mDataList);
	}
			
	public static class GenSelectAllListItemView extends GenView{
		LayoutInflater inflater;
		
		@Override
		protected View genBody(Context context, View convertView,
				final Object element, final int position) {
						
			if (inflater == null) {
				inflater = LayoutInflater.from(context);
			}
			final ViewHolder holder;
			if (convertView != null	&& convertView.getTag() instanceof ViewHolder) 
			{
				holder = (ViewHolder) convertView.getTag();
			} else 
			{
				convertView = inflater.inflate(R.layout.treesingle_row, null);
				holder = new ViewHolder();
				holder.treeSinglName = (TextView) convertView.findViewById(R.id.tree_singl_name);
				holder.treeSinglDescription = (TextView) convertView.findViewById(R.id.tree_singl_description);
				holder.treeSinglDescDate = (TextView) convertView.findViewById(R.id.tree_singl_desc_date);
				holder.treeSinglSpname = (TextView) convertView.findViewById(R.id.tree_singl_spname);

				convertView.setTag(holder);
			}
			
			String tempString1 = ((TreeSingleBean) element).getItemName();
			if (tempString1.length()>4) { holder.treeSinglName.setText(tempString1.substring(0, 4)+"...");
			}else{ holder.treeSinglName.setText(tempString1); }
			tempString1 = null;
			
			String tempString = ((TreeSingleBean) element).getItemDescription();
			if (tempString.length()>20) { holder.treeSinglDescription.setText(tempString.substring(0, 18)+"...");
			}else{ holder.treeSinglDescription.setText(tempString); }
			tempString = null;
			
			Date date = new Date(((TreeSingleBean) element).getItemDate() * 1000L);
			DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			holder.treeSinglDescDate.setText(dateFormat.format(date));
//			holder.treeSinglSpname.setText(((TreeSingleBean) element).getTreeName());
			
			/*字數過多切字*/
			if (((TreeSingleBean) element).getTreeName().length() > 8) {
				String s = (((TreeSingleBean) element).getTreeName()).substring(0, 8)+"...";
				holder.treeSinglSpname.setText(s);
			}else{
				holder.treeSinglSpname.setText(((TreeSingleBean) element).getTreeName());
			}
			
//			/*果實圖*/
//			if (((treeInfo)element).getTotalScore() >= 1500 && ((treeInfo)element).getTotalScore() < 3000) {
//				holder.fruitImg.setImageResource(R.drawable.fruit_01);
//			} 
//			else if (((treeInfo)element).getTotalScore() >= 3000 && ((treeInfo)element).getTotalScore() < 4500) {
//				holder.fruitImg.setImageResource(R.drawable.fruit_02);
//			}
//			else if (((treeInfo)element).getTotalScore() >= 4500 && ((treeInfo)element).getTotalScore() < 6000) {
//				holder.fruitImg.setImageResource(R.drawable.fruit_03);
//			}
//			else if (((treeInfo)element).getTotalScore() >= 6000) {
//				holder.fruitImg.setImageResource(R.drawable.fruit04_ani);
//				final AnimationDrawable anidraw0 = (AnimationDrawable) holder.fruitImg.getDrawable();
//				holder.fruitImg.post(anidraw0);
//			}
			
//			/*選手圖*/
//			
//				switch (((treeInfo)element).getTreeId()) {
//				case 1:
//					holder.sportsmanImg.setImageResource(R.drawable.water_sports_04);
//					break;
//				case 2:
//					holder.sportsmanImg.setImageResource(R.drawable.water_sports_02);
//					break;
//				case 3:
//					holder.sportsmanImg.setImageResource(R.drawable.water_sports_05);
//					break;
//				case 4:
//					holder.sportsmanImg.setImageResource(R.drawable.water_sports_01);
//					break;	
//				case 5:
//					holder.sportsmanImg.setImageResource(R.drawable.water_sports_03);
//					break;	
//				case 6:
//					holder.sportsmanImg.setImageResource(R.drawable.archery_m);
//					break;	
//				case 9:
//					holder.sportsmanImg.setImageResource(R.drawable.archery_03);
//					break;
//				case 10:
//					holder.sportsmanImg.setImageResource(R.drawable.track_and_field_01);
//					break;
//				case 11:
//					holder.sportsmanImg.setImageResource(R.drawable.track_and_field_02);
//					break;
//				case 12:
//					holder.sportsmanImg.setImageResource(R.drawable.shooting_01);
//					break;
//				case 13:
//					holder.sportsmanImg.setImageResource(R.drawable.shooting_03);
//					break;
//				case 14:
//					holder.sportsmanImg.setImageResource(R.drawable.shooting_02);
//					break;
//				case 15:
//					holder.sportsmanImg.setImageResource(R.drawable.table_tennis_01);
//					break;
//				case 16:
//					holder.sportsmanImg.setImageResource(R.drawable.table_tennis_02);
//					break;
//				case 17:
//					holder.sportsmanImg.setImageResource(R.drawable.taekwondo_02);
//					break;
//				case 18:
//					holder.sportsmanImg.setImageResource(R.drawable.taekwondo_01);
//					break;
//				case 19:
//					holder.sportsmanImg.setImageResource(R.drawable.taekwondo_03);
//					break;
//				case 20:
//					holder.sportsmanImg.setImageResource(R.drawable.weight_lifting_01);
//					break;
//				case 21:
//					holder.sportsmanImg.setImageResource(R.drawable.weight_lifting_02);
//					break;
//				case 22:
//					holder.sportsmanImg.setImageResource(R.drawable.weight_lifting_m);
//					break;
//				default:
//					
//					break;
//				}
				
			
//			Message msg = mhandler.obtainMessage(TreeActivity.ADAPTER_NOTIFY_DATA);
//			mhandler.sendMessage(msg);
			//TODO animation不閃
	//			holder.imgView.setOnClickListener(new View.OnClickListener() {
//				@Override
//				public void onClick(View v) {
//					mDataList.remove(position);
//					Message msg = mhandler.obtainMessage(0 , element);
//					mhandler.sendMessage(msg);
//				}
//			});

			return convertView;
		}
		
		private class ViewHolder {
			TextView treeSinglName;
			TextView treeSinglSpname;
			TextView treeSinglDescription;
			TextView treeSinglDescDate;
//			ImageView fruitImg;
		}
		
//		private String treeTypeToString(int type){
//			String typeString="";
//			switch (type) {
//			case 1:
//				typeString ="水上運動";
//				break;
//			case 2:
//				typeString ="射箭";
//				break;
//			case 3:
//				typeString ="田徑";
//				break;
//			case 4:
//				typeString ="射擊";
//				break;
//			case 5:
//				typeString ="桌球";
//				break;
//			case 6:
//				typeString ="跆拳道";
//				break;
//			case 7:
//				typeString ="舉重";
//				break;
//			}
//				return typeString;
//		}
	}
	
}
